package com.weiyi.weiman.api;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.weiyi.weiman.annotation.Log;
import com.weiyi.weiman.domain.dto.ComicsChapterDTO;
import com.weiyi.weiman.domain.dto.ComicsPictureDTO;
import com.weiyi.weiman.domain.dto.ResultInfo;
import com.weiyi.weiman.exception.APIException;
import com.weiyi.weiman.exception.TokenException;
import com.weiyi.weiman.service.comics.ComicsListService;
import com.weiyi.weiman.service.comics.ComicsShowService;
import com.weiyi.weiman.utils.ResultUtils;
import comics.BaseComics;
import comics.Comics;
import comics.ComicsCarousel;
import comics.ComicsChapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author zhang
 * @Date 2021/5/9 下午9:53
 * @Description
 * @Version 1.0
 **/
@RestController
@RequestMapping("/comics")
public class ComicsController {

    @Autowired
    private ComicsShowService comicsShowService;
    @Autowired
    private ComicsListService comicsListService;

    /**
     * 获取漫画章节信息
     * @param comicsId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/chapterList/{comicsId}/{pageNum}/{pageSize}")
    public ResultInfo getChapterList(@PathVariable("comicsId") long comicsId,
                                     @PathVariable("pageNum") int pageNum,
                                     @PathVariable("pageSize") int pageSize) {
        List<ComicsChapter> chapterList = null;
        try {
            chapterList = comicsShowService.getChapterList(comicsId, pageNum, pageSize);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(chapterList);
    }

    /**
     * 查找漫画
     * @param comicsName
     * @return
     */
    @GetMapping("/search/{comicsName}")
    public ResultInfo searchComics(@PathVariable("comicsName") String comicsName) {
        List<BaseComics> comicsList;
        try {
            comicsList = comicsShowService.selectByName(comicsName);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(comicsList);
    }

    /**
     * 获取某章节的全部图片
     * @param chapterId
     * @param request
     * @return
     */
    @Log
    @GetMapping("/show/{chapterId}")
    public ResultInfo showComics(@PathVariable("chapterId") long chapterId,
                                 HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        List<ComicsPictureDTO> comicsPictureDTOS = null;
        try {
            comicsPictureDTOS = comicsShowService.showComics(chapterId, token);
        } catch (TokenException e) {
            return ResultUtils.failureInfo(e.getTokenStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(comicsPictureDTOS);
    }

    /**
     * 获取漫画章节信息
     * @param chapterId
     * @return
     */
    @GetMapping("/chapter/{chapterId}")
    public ResultInfo getChapter(@PathVariable("chapterId") long chapterId) {
        ComicsChapterDTO chapter = null;
        try {
            chapter = comicsShowService.getChapter(chapterId);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(chapter);
    }

    /**
     * 获取轮播图信息
     * @return
     */
    @GetMapping("/carousel")
    public ResultInfo getCarouselList() {
        List<ComicsCarousel> list = null;
        try {
            list = comicsShowService.getCarouselList();
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(list);
    }

    /**
     * 获取某漫画的主页信息
     * @param comicsId
     * @return
     */
    @GetMapping("/homePage/{comicsId}")
    public ResultInfo getHomePage(@PathVariable("comicsId") long comicsId, String referer) {
        Comics comics = null;
        try {
            comics = comicsShowService.getHomePage(comicsId, referer);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(comics);
    }

    @GetMapping("/topSearch/{pageNum}/{pageSize}")
    public ResultInfo getTopSearch(@PathVariable("pageNum") int pageNum,
                                   @PathVariable("pageSize") int pageSize) {
        List<BaseComics> hotSearch = null;
        try {
            hotSearch = comicsListService.getHotSearch(pageNum, pageSize);
        } catch (APIException e) {
            return ResultUtils.failureInfo(e.getResultStatus());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtils.failureInfo(e.getMessage());
        }
        return ResultUtils.successInfo(hotSearch);
    }

}
